package com.itlong.cloud.controller.parking;

import com.itlong.cloud.POJO.DTO.operate.OperateParkingLotManageInfoDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateParkingLotManagePageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.project.operate.IOperateParkingLotManageServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      运营-智慧停车-停车场管理。
 * <desc/>
 *
 * @createDate 2018-11-05.
 */
@RestController
@RequestMapping("/parking/parkingLotManage")
public class OperateParkingLotManageController {

    private static final Logger LOG = LoggerFactory.getLogger(OperateParkingLotManageController.class);

    @Autowired
    IOperateParkingLotManageServiceClient iOperateParkingLotManageServiceClient;

    /**
     * <desc>
     *      停车场信息分页查询
     * <desc/>
     *
     * @param operateParkingLotManagePageDTO 分页查询参数
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(path = "/getParkingLotPage", method = RequestMethod.POST)
    public Object getParkingLotPage(OperateParkingLotManagePageDTO operateParkingLotManagePageDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateParkingLotManagePageDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateParkingLotManagePageDTO.class,request);
            //请求对象不能为空
            if(null == operateParkingLotManagePageDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateParkingLotManageServiceClient.getParkingLotPage(operateParkingLotManagePageDTO));
        }catch (Exception e){
            throw new DataAccessException("【运营_智慧停车_停车场信息分页查询getPlanInfoPage】停车场信息分页查询失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      添加
     * <desc/>
     *
     * @param operateParkingLotManageInfoDTO 停车场信息
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.PARKING_LOT_MANAGE_SAVE_LOG_DESC)
    @RequestMapping(path = "/save", method = RequestMethod.POST)
    public Object save(OperateParkingLotManageInfoDTO operateParkingLotManageInfoDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateParkingLotManageInfoDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateParkingLotManageInfoDTO.class,request);
            //请求对象不能为空
            if(null == operateParkingLotManageInfoDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //项目编号不能为空
            if(StringUtils.isBlank(operateParkingLotManageInfoDTO.getProjectId())){
                return new MessageVO(OperateErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }
            //关联方案不能为空
            if(StringUtils.isBlank(operateParkingLotManageInfoDTO.getPlanId())){
                return new MessageVO(OperateErrorCodeEnum.PARKING_LOT_MANAGE_PLANID_EMPTY_ERR.getErrorCode());
            }
            //判断该项目是否已有停车场
            if(iOperateParkingLotManageServiceClient.checkParkingRepeat(operateParkingLotManageInfoDTO.getProjectId()) > 0){
                return new MessageVO(OperateErrorCodeEnum.PARKING_LOT_MANAGE_REPEAT_ERR.getErrorCode());
            }
            iOperateParkingLotManageServiceClient.save(operateParkingLotManageInfoDTO);
            messageVO = new MessageVO(operateParkingLotManageInfoDTO.getProjectId()+"|"+operateParkingLotManageInfoDTO.getPlanId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_停车场管理添加停车场save】添加失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      更新
     * <desc/>
     *
     * @param operateParkingLotManageInfoDTO 停车场信息
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.PARKING_LOT_MANAGE_UPDATE_LOG_DESC)
    @RequestMapping(path = "/update", method = RequestMethod.POST)
    public Object update(OperateParkingLotManageInfoDTO operateParkingLotManageInfoDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateParkingLotManageInfoDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateParkingLotManageInfoDTO.class,request);
            //请求对象不能为空
            if(null == operateParkingLotManageInfoDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //请求对象不能为空
            if(null == operateParkingLotManageInfoDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //停车场编号不能为空
            if(StringUtils.isBlank(operateParkingLotManageInfoDTO.getParkId())){
                return new MessageVO(OperateErrorCodeEnum.PARKING_LOT_MANAGE_PARKID_EMPTY_ERR.getErrorCode());
            }
            //关联方案不能为空
            if(StringUtils.isBlank(operateParkingLotManageInfoDTO.getPlanId())) {
                return new MessageVO(OperateErrorCodeEnum.PARKING_LOT_MANAGE_PLANID_EMPTY_ERR.getErrorCode());
            }
            iOperateParkingLotManageServiceClient.update(operateParkingLotManageInfoDTO);
            messageVO = new MessageVO(operateParkingLotManageInfoDTO.getParkId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_智慧停车更新停车场update】更新失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      删除
     * <desc/>
     *
     * @param parkId 停车场编号
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @ResponseBody
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.PARKING_LOTN_MANAGE_DELETE_LOG_DESC)
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    public Object delete(String parkId){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            parkId=request.getAttribute("parkId")==null?null:request.getAttribute("parkId").toString();
            //停车场编号不能为空
            if(StringUtils.isBlank(parkId)){
                return new MessageVO(OperateErrorCodeEnum.PARKING_LOT_MANAGE_PARKID_EMPTY_ERR.getErrorCode());
            }
            //判断停车场是否使用
            if (iOperateParkingLotManageServiceClient.checkParkingLot(parkId) > 0) {
                return new MessageVO(OperateErrorCodeEnum.PARKING_LOT_MANAGE_IS_USE_ERR.getErrorCode());
            }
            iOperateParkingLotManageServiceClient.delete(parkId);
            messageVO = new MessageVO(parkId,BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_智慧停车删除停车场delete】删除失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      根据停车场编号获取停车场信息
     * <desc/>
     *
     * @param parkId 停车场编号
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @ResponseBody
    @RequestMapping(path = "/getDetail", method = RequestMethod.POST)
    public Object getDetail( String parkId){
        MessageVO messageVO;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            parkId=request.getAttribute("parkId")==null?null:request.getAttribute("parkId").toString();
            //方案编号不能为空
            if(StringUtils.isBlank(parkId)){
                return new MessageVO(OperateErrorCodeEnum.PARKING_LOT_MANAGE_PARKID_EMPTY_ERR.getErrorCode());
            }
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iOperateParkingLotManageServiceClient.getDetail(parkId));
        }catch (Exception e){
            throw new DataAccessException("【运营_智慧停车_根据停车场编号获取停车场信息getDetail】根据停车场编号获取停车场信息失败",e);
        }
        return messageVO;
    }

}
